import CozeModelClass from "../models/cozeModel.mjs";

//适配器层，调用不同底层大模型API
//type:
//     1.Coze API
//     2.DeepSeek API
//     3.Kimi API
//     ......
class  ChatServiceClass{
    //type标识调用的是那个大模型的API
    constructor(type,token) {
        this.modelIns=ChatServiceClass.getModelInstance(type,token);
        // this.token=token;
    }
    static getModelInstance(type,token){
            switch(Number(type)){
                case 1:
                    return CozeModelClass.getCozeModelInstance(token);
                default:
                    throw new Error(`Chat failed: 暂时不支持此模型，请更换模型`);
            }
    }
    async chatStream(info) {
        try {
            //若不同API返回结果不一样，可以在此层（适配层）规范化一样的返回内容
            //stream为异步生成器
            //COZE API已经封装，{event，data}
            const stream = await this.modelIns.chatStream(info);
            return this.formatResponse(stream)
        } catch (error) {
            return this.formatError(error);
        }
    }

    formatResponse(result) {
        return {
            success: true,
            data: {
                res:result,
                //简单设置，只有成功设置状态码
                code:200,
                msg:"success"
            }

        };
    }

    formatError(error) {
        return {
            success: false,
            data:{
                msg: error.message,
                code:4000,
                res:{}
            }
        };
    }

}
export default ChatServiceClass;

